package imoney.shared.model;

import java.util.ArrayList;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;

/**
 * Represents the total debt between the banker and the client. So there should be one debt object per person in each group,
 * minus the banker (n-1 debts). This class pairs with Transaction.java, and more precisely it sums up all the transactions
 * to give an own or dued amount of money.
 * Debt objects aim to be persistent and evolving over time. They are the window to every person financial situation
 * in the group.
 * 
 * Note : the algebric amount represents the sum the banker owes to the client. Thus a negative number indicates that the client
 * has to pay. This amount could also be seen as a bank account.
 * 
 * @author Arnaud PELOQUIN
 *
 */
@PersistenceCapable
public class Debt {
	// TODO : extends Registrable, no ?
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key key;
	@Persistent
	private int client;
	@Persistent
	private double amount;
	@Persistent
	private ArrayList<Integer> transactionsIDs;

	public Debt(int client, ArrayList<Integer> list){
		transactionsIDs = list;
		this.client = client;
		amount = 0;
	}
	
	public Debt(int client){
		this(client, new ArrayList<Integer>());
		
	}
	/**
	 * Consider using <code>Debt(int client)</code> instead.
	 * Or <code>Debt(int client, ArrayList-Integer transactionsList)</code>
	 */
	public Debt(){
		this(-10);
	}
	
	// GETTERS & SETTERS ---------------------------------
	public void setKey(Key key) {
		this.key = key;
	}
	public Key getKey() {
		return key;
	}
	public void setClient(int client) {
		this.client = client;
	}
	public int getClient() {
		return client;
	}
	public double getMontant() {
		return amount;
	}
	

	public ArrayList<Integer> getTransactionsIDs() {
		return transactionsIDs;
	}
	
	// Specific Methods --------------------------------------
	/** Add the specified amount to the debt. Can be a positive or negative double. See Debt javadoc for full details
	 * @param montant
	 * @param transaction ID: add the ID if it's positive, removes its opposite if it's negative.
	 */
	public void addAmount(double sum, int transactionID) {
		amount += sum;
		if(transactionID<0){
			transactionsIDs.remove((Integer)transactionID);
		}
		else{
			transactionsIDs.add(transactionID);
		}
	}
	
	public boolean hasRegisteredTransaction(int transacID){
		return transactionsIDs.contains(transacID);
	}
	
	public String toString(){
		return "Client: "+client+", Amount: "+amount+", Transactions: "+transactionsIDs;
	}
}
















